/// \file blockallocator.h.
///============================================================================\n
/// Name        : blockallocator.h\n
/// Author      : Gergely Lukacsy (info@dlog-reasoner.org)\n
/// Version     : 0.01\n
/// Copyright   : DERI\n
/// Description : Simple memory blockallocator, header file.\n
/// 
/// This file is part of the DParser and DLog 2 projects.\n
///============================================================================

#ifndef BLOCKALLOCATOR_H_
#define BLOCKALLOCATOR_H_

/// A very simple block allocator used for small object creation.
class BLOCKALLOCATOR {
public:
	/// Constructor.
	/// @param nrRequestedBlockSize is the size of a block in bytes.
	BLOCKALLOCATOR(size_t nrRequestedBlockSize);

	/// Destructor.
	~BLOCKALLOCATOR();

	/// Allocator.
	/// @param nrBytesToAllocate specifies the number of bytes we would like to allocate.
	/// @return A pointer to the starting point of a free space on the heap large enough to stor nSizeToAllocate bytes.
	void* allocate(size_t nrBytesToAllocate);

	/// Size of a block in bytes.
	DWORD nBlockSize;
private:

	/// Total number of blocks managed by this allocator.
	DWORD nrBlocks;

	/// The current block identifier.
	DWORD nCurrentBlock;

	/// Index of the current block;
	DWORD nCurrentIndex;

	/// This is the table where the blocks are stored.
	BYTE** mBlocks;
};

// Specialise new for BLOCKALLOCATOR.
inline void* operator new(size_t nrBytes, BLOCKALLOCATOR& pool) {
  return pool.allocate(nrBytes);
}

// Only to disable warning c4291
inline void operator delete(void* p, BLOCKALLOCATOR& pool) {
	ASSERT(false);
}

inline void* operator new[](size_t nrBytes, BLOCKALLOCATOR& pool) {
  return pool.allocate(nrBytes);
}

#endif /* BLOCKALLOCATOR_H_ */
